module V1
  class Videos < Grape::API

    get 'video/categories' do
      render_json VideoCategory.all
    end

    params do
      optional :video_category_id
    end
    get 'video/list' do
      if params[:video_category_id]
        render_json VideoCategory.find_by(id:params[:video_category_id]).video_groups.page(params[:page]).per(params[:per])
      else
        render_json VideoGroup.all.page(params[:page]).per(params[:per])
      end
    end

    params do
      optional :id
    end
    get 'video_group/detail' do
      video_group = VideoGroup.find_by(id:params[:id])
      render_json video_group: video_group, video_list: video_group.videos.order("position asc")
    end

    params do
      optional :id
    end
    get 'video/detail' do
      render_json Video.find_by(id:params[:id])
    end

    params do
      optional :title
    end
    post 'video/search' do
      ransack_p = {}
      ransack_p[:title] = params[:title]
      render_json VideoGroup.where("title like ?", "%#{ransack_p[:title]}%").page(params[:page]).per(params[:per])
    end
    
  end
end

